package com.bianguo.android.functiondemo.view.activity;

import android.database.Cursor;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bianguo.android.functiondemo.R;
import com.bianguo.android.functiondemo.adapter.AlbumVideoAdapter;
import com.bianguo.android.functiondemo.adapter.BaseRecyclerAdapter;
import com.bianguo.android.functiondemo.bean.AlbumData;
import com.bianguo.android.functiondemo.utils.MLog;
import com.bianguo.android.functiondemo.videocompressor.VideoCompress;

import java.io.File;
import java.util.ArrayList;

import butterknife.BindView;

public class VideoCompressActivity extends BaseActivity implements BaseRecyclerAdapter.OnClickItemListener {

    @BindView(R.id.video_recycler)
    RecyclerView videoRecycler;

    private GridLayoutManager layoutManager;
    private ArrayList<AlbumData> albumDatas;
    private AlbumVideoAdapter adapter;

    @Override
    public int layoutId() {
        return R.layout.activity_video_compress;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        layoutManager = new GridLayoutManager(mContext, 4);
        videoRecycler.setLayoutManager(layoutManager);
        albumDatas = new ArrayList<>();
        adapter = new AlbumVideoAdapter(mContext, albumDatas);
        adapter.setOnClickItemListener(this);
        videoRecycler.setAdapter(adapter);
        initData();
    }

    private void initData() {
        ArrayList<AlbumData> albums = new ArrayList<>();
        String[] columns = {MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID};
        String orderBy = MediaStore.Video.Media.DATE_TAKEN;
        Cursor videoCursor = mContext
                .getApplicationContext()
                .getContentResolver()
                .query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns,
                        null, null, orderBy + " DESC");
        int count = videoCursor.getCount();
        for (int i = 0; i < count; i++) {
            videoCursor.moveToPosition(i);
            int dataColumnIndex = videoCursor
                    .getColumnIndex(MediaStore.Video.Media.DATA);
            String videoUrl = videoCursor.getString(dataColumnIndex);
            File file = new File(videoUrl);
            if (file.exists()) {
                AlbumData album = new AlbumData();
                album.setPath(videoUrl);
                albums.add(album);
            }
        }
        adapter.addAll(albums);
    }

    @Override
    public void onClickItemListener(BaseRecyclerAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.item_album_video:
                VideoCompress.compressVideoLow(albumDatas.get(position).getPath(), "", new VideoCompress.CompressListener() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onFail() {

                    }

                    @Override
                    public void onProgress(float percent) {
                        MLog.mLog("percent = " + percent);
                    }
                });
                break;
        }
    }
}
